package xml;

import java.util.HashMap;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class GenericHandler extends DefaultHandler {

	private Map<String, String> values = new HashMap<String, String>();

	private String currentKey;

	public GenericHandler(String[] keys) {
		values = new HashMap<String, String>();
		for (String key : keys) {
			values.put(key, "");
		}
	}

	public Map<String, String> getValues() {
		return values;
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

		currentKey = qName;

	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {

		currentKey = "";

	}

	@Override
	public void characters(char ch[], int start, int length) throws SAXException {

		if (!currentKey.isEmpty()) {
			if (values.containsKey(currentKey)) {
				values.put(currentKey, new String(ch, start, length));
			}
		}

	}

	public static void main(String[] args) {

		// try {
		// Map<String, String> values = new ReadXMLFile().parse(new FileInputStream(new File("update_info.xml")));
		//
		// for (String key : values.keySet()) {
		// System.out.println(key + " " + values.get(key));
		// }
		// } catch (FileNotFoundException fnfe) {
		// fnfe.printStackTrace();
		// }

	}

}
